package com.chenxin.quartz.task.mes;

import com.chenxin.common.utils.ConvertUtils;
import com.chenxin.interact.domain.UrlMap;
import com.chenxin.interact.domain.wmsToMes.WmsToMesQuality;
import com.chenxin.interact.domain.mesNotice.QualityNoticeDTO;
import com.chenxin.interact.handler.mesHandler.MesQualityHandler;
import com.chenxin.interact.service.IUrlMapService;
import com.chenxin.interact.service.IWmsToMesQualityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("qualityNoticeTask")
@Slf4j
public class QualityNoticeTask {
    @Autowired
    private IUrlMapService urlMapService;
    @Autowired
    private MesQualityHandler mesQualityHandler;
    @Autowired
    private IWmsToMesQualityService wmsToMesQualityService;

    public void run(String method) {
        UrlMap urlMap = urlMapService.selectUrlMapByUrlNo("MES_POST_URL");
        if (urlMap == null) {
            log.error("接口【MES_POST_URL】未定义!");
            return;
        }
        // 获取待上报的出库检验信息
        List<WmsToMesQuality> qualities = wmsToMesQualityService.selectWmsToMesQualityList();
        if (qualities == null || qualities.size() == 0) {
            return;
        }
        // 数据转换
        List<QualityNoticeDTO> qualityNotices = ConvertUtils.sourceToTarget(qualities, QualityNoticeDTO.class);
        try {
            mesQualityHandler.notice(urlMap, qualityNotices, method);
        } catch (Exception e) {
            log.error("MES质检通知异常:{}", e);
        }
    }
}
